package com.fas.system.controller;

import com.fas.common.annotation.Log;
import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.domain.model.LoginUser;
import com.fas.common.core.page.TableDataInfo;
import com.fas.common.enums.BusinessType;
import com.fas.common.utils.SecurityUtils;
import com.fas.common.utils.poi.ExcelUtil;
import com.fas.system.domain.FasEquipment;
import com.fas.system.domain.FasEquipmentStatusHistory;
import com.fas.system.domain.vo.EquipmentVo;
import com.fas.system.service.IFasEquipmentService;
import com.fas.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备信息Controller
 *
 * @author ruoyi
 * @date 2022-05-05
 */
@Api(tags = "设备信息")
@RestController
@RequestMapping("/system/equipment")
public class FasEquipmentController extends BaseController {
    @Autowired
    private IFasEquipmentService fasEquipmentService;


    @Autowired
    private ISysDeptService deptService;

    @ApiOperation(value = "子公司查询设备")
//    @PreAuthorize("@ss.hasPermi('system:equipment:OrArea')")
    @GetMapping("/findLikeCompanyOrArea")
    public TableDataInfo findLikeCompanyOrArea(@RequestParam(value = "condition", required = false) String condition
            , @RequestParam(value = "deptId", required = false) Long deptId) {
        startPage();
        FasEquipment fasEquipment = new FasEquipment();
        fasEquipment.setDeptId(deptId);
        fasEquipment.setCondition(condition);
        List<FasEquipment> allEquipment = fasEquipmentService.findLikeEngineer(fasEquipment);
        System.out.println(allEquipment);
        return getDataTable(allEquipment);
    }


    @ApiOperation(value = "微信设备查询")
//    @PreAuthorize("@ss.hasPermi('system:equipment:wxment')")
    @GetMapping("/wxfindallequment")
    public TableDataInfo wxfindallequment(Integer pageNum, Integer pageSize,String buildingId) {
        startPage();
        LoginUser user = SecurityUtils.getLoginUser();
        FasEquipment fasEquipment = new FasEquipment();
        fasEquipment.setDeptId(user.getDeptId());
        fasEquipment.setBuildingid(buildingId);
        System.out.println(fasEquipment);
        List<FasEquipment> allEquipment = fasEquipmentService.miniappEquipmentList(fasEquipment);
        return getDataTable(allEquipment);
    }


    @ApiOperation(value = "设备详细信息查询")
    @GetMapping("/equipmentdetails")
    public TableDataInfo equipmentdetails(String equipmentNumber) {
        startPage();
        List<FasEquipment> equipmentdetails = fasEquipmentService.equipmentdetails(equipmentNumber);
        System.out.println(equipmentdetails);
        return getDataTable(equipmentdetails);
    }


    /**
     * 查询设备信息列表
     */
    @ApiOperation(value = "查询设备信息列表")
//    @PreAuthorize("@ss.hasPermi('system:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(FasEquipment fasEquipment) {
        startPage();
        List<FasEquipment> list = fasEquipmentService.selectFasEquipmentList(fasEquipment);
        return getDataTable(list);
    }


    /* ----------------------------------------新增或修改的接口------------------------------------------------------------*/
    @ApiOperation(value = "总公司查询设备")
//    @PreAuthorize("@ss.hasPermi('system:equipment:ceshi')")
    @GetMapping("/findAllEquipment")
    public TableDataInfo findAllEquipment(FasEquipment fasEquipment, Integer pageNum, Integer pageSize) {
        System.out.println("输入的参数：" + fasEquipment.toString());
        LoginUser user = SecurityUtils.getLoginUser();
        SysDept sysDept = deptService.selectDeptById(user.getDeptId());
//        DeptType :  0:总部 1:渠道商 2:维保单位
        if ("0".equals(sysDept.getDeptType())) {
            startPage();
            List<FasEquipment> identityfind = fasEquipmentService.findAllEquipment(fasEquipment);
            System.out.println(identityfind);
            return getDataTable(identityfind);
        } else if ("2".equals(sysDept.getDeptType())) {
            fasEquipment.setDeptId(user.getDeptId());
            startPage();
            List<FasEquipment> allEquipment = fasEquipmentService.findLikeEngineer(fasEquipment);
//            startPage();
//            List<FasEquipment> allEquipment = fasEquipmentService.findAllEquipment(condition,deptId);
//            return getDataTable(allEquipment);
            return getDataTable(allEquipment);
        }
        return null;
    }

    /**
     * 导出设备信息列表
     */
    @ApiOperation(value = "导出设备信息列表")
//    @PreAuthorize("@ss.hasPermi('system:equipment:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody FasEquipment fasEquipment) {
        System.out.println("输入的参数：" + fasEquipment.toString());
        LoginUser user = SecurityUtils.getLoginUser();
        SysDept sysDept = deptService.selectDeptById(user.getDeptId());
        List<FasEquipment> fasEquipmentList = new ArrayList<>();
        if ("0".equals(sysDept.getDeptType())) {
        fasEquipmentList = fasEquipmentService.findAllEquipment(fasEquipment);
        } else if ("2".equals(sysDept.getDeptType())) {
            fasEquipment.setDeptId(user.getDeptId());
            fasEquipmentList = fasEquipmentService.findLikeEngineer(fasEquipment);
        }
        ExcelUtil<FasEquipment> util = new ExcelUtil<FasEquipment>(FasEquipment.class);
        util.exportExcel(response, fasEquipmentList, "设备信息数据");
    }

    @PostMapping("/importData")
    @Log(title = "设备信息", businessType = BusinessType.IMPORT)
    public AjaxResult importData(MultipartFile file) {
        try {
            ExcelUtil<FasEquipment> util = new ExcelUtil<>(FasEquipment.class);
            List<FasEquipment> equipmentList = util.importExcel(file.getInputStream());
            equipmentList.forEach(e -> e.setDeptId(getDeptId()));
            return fasEquipmentService.importFasEquipment(equipmentList);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 暂时不用
     */
    @ApiOperation(value = "维保单位查询设备")
//    @PreAuthorize("@ss.hasPermi('system:equipment:mainte')")
    @GetMapping("/findLikeEngineer")
    public TableDataInfo findLikeEngineer(FasEquipment fasEquipment) {
        startPage();
        LoginUser user = SecurityUtils.getLoginUser();
        fasEquipment.setDeptId(user.getDeptId());
        List<FasEquipment> allEquipment = fasEquipmentService.findLikeEngineer(fasEquipment);
        System.out.println(allEquipment);
        return getDataTable(allEquipment);
    }

    /**
     * 暂时不用
     */
    @ApiOperation(value = "查询设备管理首页数据")
    @PostMapping("/findAll")
    public TableDataInfo findAll(FasEquipment fasEquipment) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fasEquipment.setDeptId(loginUser.getDeptId());
        startPage();
        return getDataTable(fasEquipmentService.findAll(fasEquipment));
    }

    /**
     * 获取设备信息详细信息
     */
    @ApiOperation(value = "获取设备信息详情信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return AjaxResult.success(fasEquipmentService.selectFasEquipmentById(id, loginUser.getDeptId()));
    }


    /**
     * 新增设备信息
     */
    @ApiOperation(value = "新增设备信息")
//    @PreAuthorize("@ss.hasPermi('system:equipment:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FasEquipment fasEquipment) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fasEquipment.setDeptId(loginUser.getDeptId());
        return toAjax(fasEquipmentService.insertFasEquipment(fasEquipment));
    }

    /**
     * 修改设备信息
     */
    @ApiOperation(value = "修改设备信息")
//    @PreAuthorize("@ss.hasPermi('system:equipment:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FasEquipment fasEquipment) {
        return toAjax(fasEquipmentService.updateFasEquipment(fasEquipment));
    }

    /**
     * 删除设备信息
     */
    @ApiOperation(value = "删除设备信息")
//    @PreAuthorize("@ss.hasPermi('system:equipment:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fasEquipmentService.deleteFasEquipmentByIds(ids));
    }

    @ApiOperation(value = "更新设备状态")
    @PostMapping("/updateDeviceStatus")
    public AjaxResult updateDeviceStatus(@RequestBody EquipmentVo equipmentVo) {
        AjaxResult result = fasEquipmentService.updateDeviceStatus(equipmentVo);
        return result;
    }

    @ApiOperation(value = "首页报警设备信息是否已读")
    @GetMapping("/read")
    public AjaxResult read(Integer alarmId) {
        return toAjax(fasEquipmentService.read(alarmId));
    }

    /**
     * 历史记录-设备运行数据
     *
     * @param history
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "历史记录-设备运行数据")
    @PostMapping("/listDevicePointData")
    public TableDataInfo listDevicePointData(@RequestBody FasEquipmentStatusHistory history, Integer pageNum, Integer pageSize) {
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")) {
            if (history.getDeptId() == null) {
                history.setDeptId(deptId.intValue());
            }
        }
        startPage();
        return getDataTable(fasEquipmentService.listDevicePointData(history));
    }

    @ApiOperation(value = "首页报警未读设备报警信息")
    @GetMapping("/unreadList")
    public AjaxResult unreadList() {
        return AjaxResult.success(fasEquipmentService.unreadList(null));
    }


}
